/* 
 * File:   DatabaseManager.cpp
 * Author: toantm
 * 
 * Created on September 20, 2012, 9:19 PM
 */

#include "DatabaseManager.h"

DatabaseManager::DatabaseManager() {
    logger = &Logger::get("DatabaseManager");
}

DatabaseManager::DatabaseManager(const DatabaseManager& orig) {
}

DatabaseManager::~DatabaseManager() {
}

bool DatabaseManager::copyDBFromHashDBtoGrassDB(string path, GrassDB &grassDB) {
    HashDB hashDB;
    //    GrassDB *grassDB1=new GrassDB() ;
    //string path="../database/";
    //string path = bmPocoServer.getPathDatabase();
    //path.append(fileName);
    if (!hashDB.open(path, HashDB::OWRITER | HashDB::OCREATE)) {
        cout << "path : " << path << endl;
        cerr << "Open error 1: " << hashDB.error().name() << endl;
        poco_error_f1(*logger, "Open error 1: %s", hashDB.error().name());
        return false;
    }

    poco_information_f1(*logger, "Open %s", path);

    if (!grassDB.open("", GrassDB::OWRITER | GrassDB::OCREATE)) {
        cerr << "Open error GrassDB: " << grassDB.error().name() << endl;
        poco_error_f1(*logger, "Open error GrassDB: %s", grassDB.error().name());
        return false;
    }

    DB::Cursor* cur = hashDB.cursor();
    cur->jump();
    string ckey, cvalue;

    while (cur->get(&ckey, &cvalue, true)) {
        grassDB.set(ckey,cvalue);
    }
//    DB::Cursor* cur2 = grassDB.cursor();
//    cur2->jump();
//    string ckey, cvalue;
//
//    while (cur2->get(&ckey, &cvalue, true)) {
//        cout << ckey << " : " << cvalue << endl;
//    }

    poco_information_f1(*logger, "Copy complete from HashDB %s to GrassDB", path);
    cout << "Copy complete from HashDB " << path << " to GrassDB" << endl;
    poco_information_f1(*logger, "Close %s", path);
    cout << "Close " << path << endl;
    hashDB.close();
    return true;
}

